<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>
<span id="show" style="display:none">123</span>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-mini" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-mini layui-btn-normal" lay-event="edit">编辑</a>
</script>


<script src="/static/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->


<script>
    layui.use(['table', 'laypage', 'layer','jquery', 'form', 'laydate'], function() {
        var table = layui.table;
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;

        table.render({

            elem: '#test',
            url: '/creative/getAdvisers',
            toolbar: '#toolbarDemo',
            title: '用户数据表',
            totalRow: true,
            cols: [
                [{
                    type: 'checkbox',
                    fixed: 'left'
                }, {
                    // totalRowText: '合计',
                    field: 'id',
                    title: '导师编号',
                    width: 180,
                    // fixed: 'left',
                    unresize: true,
                    sort: true,
                }, {
                    field: 'name',
                    title: '导师姓名',
                    width: 150,
                    edit: 'text'
                }, {
                    field: 'tel',
                    title: '导师联系方式',
                    width: 200,
                    sort: true,
                    // totalRow: true
                }, {
                    field: 'info',
                    title: '导师信息',
                    width: 350,
                    edit: 'text',
                    sort: true
                }, {
                    field: 'department',
                    title: '学院',
                    width: 200
                }, {
                    field: 'avatar',
                    title: '头像url',
                    width: 220
                }, {
                    field: 'email',
                    title: '邮箱',
                    width: 220
                }, {
                    field: 'office',
                    title: '办公地点',
                    width: 220
                }, {
                    field: 'education',
                    title: '教育经历',
                    width: 220
                }, {
                    field: 'position',
                    title: '现任职位',
                    width: 220
                }, {
                    field: 'teachingCourse',
                    title: '主讲课程',
                    width: 220
                }, {
                    field: 'researchDirection',
                    title: '研究方向',
                    width: 220
                }, {
                    fixed: 'right',
                    width: 178,
                    align: 'center',
                    toolbar: '#barDemo'
                }]
            ],
            page: false,
        });

        var arr = [];
        table.on('checkbox(test)', function(obj) {
            var flg = 0;
            if (obj.type != 'all') {
                if (arr.length == 0) {
                    if (obj.checked == true) {
                        arr.push(obj);

                    }
                } else {
                    $.each(arr, function(index, value) {
                        if (obj.data.id == value.data.id) {
                            flg = 1;
                            if (obj.checked == false) {
                                arr.splice(index, 1);
                            }
                        } else {
                            flg = 0;
                        }

                    });
                    if (flg == 0) {
                        arr.push(obj);
                    }
                }
            }
        });

        //工具栏事件
        table.on('toolbar(test)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);

            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    $(this).parent().parent().parent().parent().toggle();
                    $("#show").toggle();
                    $("#all").toggle();
                    console.log(data)
                    $('#show').html(JSON.stringify(data))
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    console.log(checkStatus)
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选')
                    break;
            };
        });


        //监听行工具事件
        table.on('tool(test)', function(obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function(index) {
                    // console.log(obj);
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                    layer.open({
                        //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        type: 1,
                        title: "修改导师信息",
                        area: ['800px', '600px'],
                        content: $("#popUpdateTest")//引用的弹出层的页面层的方式加载修改界面表单
                });
                    //表单初始赋值
                    setForm(data);
                    //动态向表传递赋值可以参看文章进行修改界面的更新前数据的显示，当然也是异步请求的要数据的修改数据的获取
                    setFormValue(obj,data);
                }
                console.log(obj.data);
        });

            //监听弹出框表单提交，massage是修改界面的表单数据,submit(sub)是修改按钮的绑定
            function setFormValue(obj,data){
                form.on('submit(sub)', function(massage) {
                    console.log(massage);
                    $.ajax({
                        url:'/creative/updateAdviserInfo',
                        type:'POST',
                        data:{
                               id:data.id,  //原来的数据：id
                               adviserName:massage.field.name,
                               adviserTel:massage.field.tel,
                               adviserInfo:massage.field.info,
                               department:massage.field.department,
                               avatar:massage.field.avatar,
                               email:massage.field.email,
                               office:massage.field.office,
                               education:massage.field.education,
                               position:massage.field.position,
                               teachingCourse:massage.field.teachingCourse,
                               researchDirection:massage.field.researchDirection
                        },
                        success:function (res) {
                            var returnCode = res.code;//取得返回数据（Sting类型的字符串）的信息进行取值判断
                            console.log(res.code);
                            console.log(res);
                            if(returnCode==0){
                                layer.closeAll('loading');
                                layer.load(2);
                                layer.msg("修改成功", {icon: 6});
                                setTimeout(function(){
                                    obj.update({
                                        id:data.id,
                                        name:massage.field.name,
                                        tel:massage.field.tel,
                                        info:massage.field.info,
                                        department:massage.field.department,
                                        avatar:massage.field.avatar,
                                        email:massage.field.email,
                                        office:massage.field.office,
                                        education:massage.field.education,
                                        position:massage.field.position,
                                        teachingCourse:massage.field.teachingCourse,
                                        researchDirection:massage.field.researchDirection
                                    });//修改成功修改表格数据不进行跳转
                                    layer.closeAll();//关闭所有的弹出层
                                }, 1000);
                                //加载层-风格
                            }else{
                                layer.msg("修改失败", {icon: 6});
                            }
                        }
                    })
                    return false;
                })

            }

            //表单初始赋值
        function setForm(data){
            form.val('adviser', {
                "name": data.name     // 格式："name": "value"
                ,"tel": data.tel
                ,"info":data.info
                ,"department":data.department
                ,"avatar": data.avatar
                ,"email":data.email
                ,"office":data.office
                ,"education": data.education
                ,"position":data.position
                ,"teachingCourse":data.teachingCourse
                ,"researchDirection": data.researchDirection
            })
        }
    });
</script>

<div class="layui-row" id="popUpdateTest" style="display:none;">
    <div class="layui-col-md10">
        <form   class="layui-form layui-from-pane"  lay-filter="adviser" action="" style="margin-top:20px" >

            <div class="layui-form-item">
                <label class="layui-form-label">导师姓名 </label>
                <div class="layui-input-block">
                    <input type="text" name="name"  required  lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">导师联系电话</label>
                <div class="layui-input-block">
                    <input type="text" name="tel"  required   autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">导师信息</label>
                <div class="layui-input-block">
                    <textarea name="info" required  lay-verify="required" autocomplete="off" placeholder="" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">所属学院</label>
                <div class="layui-input-block">
                    <select name="department" lay-verify="required">
                        <option value=""></option>
                        <option value="计算机工程与应用数学学院">计算机工程与应用数学学院</option>
                        <option value="土木工程学院">土木工程学院</option>
                        <option value="外国语学院">外国语学院</option>
                        <option value="机电工程学院">机电工程学院</option>
                        <option value="生物与环境工程学院">生物与环境工程学院</option>
                        <option value="电子信息与电气工程学院">电子信息与电气工程学院</option>
                        <option value="法学院">法学院</option>
                        <option value="经济与管理学院">经济与管理学院</option>
                        <option value="影视艺术与文化传播学院">影视艺术与文化传播学院</option>
                        <option value="艺术设计学院">艺术设计学院</option>
                        <option value="音乐学院">音乐学院</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">头像url</label>
                <div class="layui-input-block">
                    <input type="text" name="avatar"  required  lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="email"  required   autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">办公地点</label>
                <div class="layui-input-block">
                    <input type="text" name="office"  required   autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">教育经历</label>
                <div class="layui-input-block">
                    <input type="text" name="education"  required  lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">现任职位</label>
                <div class="layui-input-block">
                    <input type="text" name="position"  required  lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">主讲课程</label>
                <div class="layui-input-block">
                    <input type="text" name="teachingCourse"  required  lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">研究方向</label>
                <div class="layui-input-block">
                    <input type="text" name="researchDirection"  required  lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item" style="margin-top:40px">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="sub">确认修改</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>

        </form>
    </div>
</div>


</body>
</html>
